package com.jt.ktdemo.ui.mian

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.jt.ktdemo.R
import com.jt.ktdemo.adapter.MessageHomeAdapter
import com.jt.ktdemo.adapter.OnItemClickListener
import com.jt.ktdemo.bean.MessageObtaionBean
import com.jt.ktdemo.bean.ObtainMessageBean
import com.jt.ktdemo.http.HTTPAPI
import com.jt.ktdemo.ui.messsageDetail.MessageDetailActivity
import com.jt.ktdemo.utils.GsonUtils
import com.jt.ktdemo.utils.LogUtil
import com.lzy.okgo.callback.StringCallback
import okhttp3.Call
import okhttp3.Response
import java.lang.Exception

class MessageFragment : Fragment() {

    private lateinit var rlModule: RecyclerView
    private var listStrs: ArrayList<ObtainMessageBean> = ArrayList<ObtainMessageBean>()
    private lateinit var messageHomeAdapter: MessageHomeAdapter
    private var TYPE_REPORT: String = "报告消息"
    private var TYPE_SYSTEM: String = "系统消息"
    private lateinit var intent: Intent

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        var view = inflaterView(inflater, container)
        if (view != null) {
            initView(view)
        }
        return view
    }

    @SuppressLint("UseRequireInsteadOfGet")
    private fun initView(view: View) {
        rlModule = view.findViewById(R.id.rlModule)
        rlModule.layoutManager = LinearLayoutManager(activity)
        messageHomeAdapter = MessageHomeAdapter(R.layout.item_message_center, listStrs, context!!)
        rlModule.adapter = messageHomeAdapter
        messageHomeAdapter.setOnItemClickListener(object : OnItemClickListener,
            BaseQuickAdapter.OnItemClickListener {
            override fun onItemClick(pos: Int) {
                LogUtil.e("点击$pos")
            }

            override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
                intent = Intent(activity, MessageDetailActivity().javaClass)
                if ("report".equals(listStrs[position].type)) {
                    LogUtil.e("报告消息")
                    intent.putExtra("module_title", "Report messages")
                } else if ("system".equals(listStrs[position].type)) {
                    LogUtil.e("系统消息")
                    intent.putExtra("module_title", "System messages")
                }
                startActivity(intent)
            }
        })
        getMessageObtion()

    }

    private fun inflaterView(inflater: LayoutInflater, container: ViewGroup?): View? {
        return inflater.inflate(R.layout.fragment_message, container, false)
    }

    fun getMessageObtion() {
        HTTPAPI.getMessageObtion(
            object : StringCallback() {
                @SuppressLint("NotifyDataSetChanged")
                override fun onSuccess(t: String?, call: Call?, response: Response?) {
                    LogUtil.e("首页消息-->$t")
                    val bean: MessageObtaionBean =
                        GsonUtils.parse(t.toString(), MessageObtaionBean::class.java)
                    if (!bean.IsError) {
                        listStrs.clear()
                        listStrs.add(
                            ObtainMessageBean(
                            bean.messageContent?.system.toString(),bean.messageTime?.system.toString(),
                        bean.messageCount?.system.toString(),bean.messageType?.system.toString())
                        )

                        listStrs.add(ObtainMessageBean(
                            bean.messageContent?.report.toString(),bean.messageTime?.report.toString(),
                            bean.messageCount?.report.toString(),bean.messageType?.report.toString()))

                        messageHomeAdapter.notifyDataSetChanged()

                    }
                }

                override fun onError(call: Call?, response: Response?, e: Exception?) {
                    LogUtil.e("首页消息失败-->${e!!.message}")
                }
            })
    }


}